﻿using System;
using System.Collections.Generic;
using System.Data.Objects;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WeddingEventManagement_API
{
    public class DashboardManager : IDashboardManager
    {
        public List<Client> NewestClients()
        {
            using (CreativeSystemsEntities db = new CreativeSystemsEntities())
            {
                var past = DateTime.Now.Date.AddDays(-30);
                return db.Clients
                    .Where(c => c.IsActive && c.DateCreated > past)
                    .OrderBy(m => m.DateCreated)
                    .ToList();
            }
        }


        public List<Event> NewestEvents()
        {
            using (CreativeSystemsEntities db = new CreativeSystemsEntities())
            {
                var past = DateTime.Now.AddDays(-30);
                return db.Events
                    .Where(m => m.CreatedDate > past)
                    .OrderBy(m => m.CreatedDate)
                    .ToList();
            }
        }

        public List<Client> UpdatedClients()
        {
            using (CreativeSystemsEntities db = new CreativeSystemsEntities())
            {
                var past = DateTime.Now.AddDays(-30);
                return db.Clients
                    .Where( m => m.UpdateUserId.HasValue && m.LastUpdated.HasValue && m.IsActive && m.DateCreated != m.LastUpdated.Value && m.LastUpdated.Value > past)
                    .OrderBy(m => m.LastUpdated.Value)
                    .ToList();
            }
        }

        public List<Event> UpdatedEvents()
        {
            using (CreativeSystemsEntities db = new CreativeSystemsEntities())
            {
                var past = DateTime.Now.AddDays(-30);
                return db.Events
                    .Where(m => m.LastUpdatedUserID.HasValue && m.LastUpdated.HasValue && m.CreatedDate != m.LastUpdated.Value && m.LastUpdated.Value > past)
                    .OrderBy(m => m.LastUpdated.Value)
                    .ToList();
            }
        }

        public List<Location> NewestLocations()
        {
            using (CreativeSystemsEntities db = new CreativeSystemsEntities())
            {
                var past = DateTime.Now.AddDays(-30);
                return db.Locations
                    .Where(m => m.CreatedDate.HasValue && m.CreatorUserID.HasValue && m.CreatedDate.Value > past)
                    .OrderBy(m => m.CreatedDate.Value)
                    .ToList();
            }
        }

        public List<Location> UpdatedLocations()
        {
            using (CreativeSystemsEntities db = new CreativeSystemsEntities())
            {
                var past = DateTime.Now.AddDays(-30);
                return db.Locations
                    .Where(m => m.UpdatedUserID.HasValue && m.LastUpdated.HasValue && m.CreatedDate != m.LastUpdated.Value && m.LastUpdated.Value > past)
                    .OrderBy(m => m.LastUpdated.Value)
                    .ToList();
            }
        }

        public List<Client> DeletedClients()
        {
            using (CreativeSystemsEntities db = new CreativeSystemsEntities())
            {
                var past = DateTime.Now.AddDays(-30);
                return db.Clients
                    .Where(m => m.IsActive == false && m.LastUpdated.Value > past)
                    .OrderBy(m => m.LastUpdated.Value)
                    .ToList();
            }
        }

        public List<Event> DeletedEvents()
        {
            throw new NotImplementedException();
        }

        public List<Location> DeletedLocations()
        {
            throw new NotImplementedException();
        }

        public List<Payment> NewestPayments()
        {
            using (CreativeSystemsEntities db = new CreativeSystemsEntities())
            {
                var past = DateTime.Now.AddDays(-30);
                return db.Payments.Include("Invoice.Event.Client")
                    .Where(m => m.DateTime > past)
                    .OrderBy(m => m.DateTime)
                    .ToList();
            }
        }

        public List<Event> UpcomingEvents()
        {
            using (CreativeSystemsEntities db = new CreativeSystemsEntities())
            {
                var now = DateTime.UtcNow;
                return db.Events.Include("EventType")
                    .Where(e => e.StartDate >= now)
                    .OrderBy(e => e.StartDate)
                    .Take(20)
                    .ToList();
            }
        }

        public List<Invoice> OutstandingInvoices()
        {
            using (CreativeSystemsEntities db = new CreativeSystemsEntities())
            {
                var now = DateTime.UtcNow;
                var invoices = db.Invoices.Include("Payments").Where(i => i.IsEditable == true && i.DueDate <= now);
                return invoices.ToList();
            }
        }

    }
}
